/*
 * Mailer.java
 *
 * Created on September 10, 2007, 6:07 PM
 */
package edu.chl.vrangogym.utilities;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



/**
 *  
 * @author UlkLars
 */
public class Mailer {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_HOST_PORT = 465;
    private static final String SMTP_AUTH_USER = "edu.chl.loskar@gmail.com";
    private static final String SMTP_AUTH_PWD = "Web-appWebShop";
    private static final String SUBJECT = "Reservation confirmation";

    public static void sendMail(String content, String receiver) throws NoSuchProviderException, MessagingException {
        Properties props = new Properties();

        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.host", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
        // props.put("mail.smtps.quitwait", "false");

        Session mailSession = Session.getDefaultInstance(props);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject(SUBJECT);
        message.setContent(content, "text/plain");

        message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(receiver));

        transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

        transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
        transport.close();

    }
}